<?php
/**
 * Created by PhpStorm.
 * User: 王福
 * 数据抓取
 * Date: 2018/12/5
 * Time: 14:41
 */

namespace app\admin\controller\crm;
use app\common\controller\Backend;
use Think\Db;
use QL\QueryList;
class Query extends Backend
{
    /**
     * 添加B端用户根据名称全拼抓取B端数据
     * @return \think\response\Json|void
     * $id : allot_id
     */
    public function index($id = '')
    {
        //查询B端用户
        if($id){
            $linkBuser = Db::name('Buser');
            $buserData = $linkBuser->field('*')->alias('b')->where(array('id'=>$id))->select();
            if($buserData&&$buserData[0]['pinname']){
                    $url = 'http://www.dailuopan.com/pingji/all';
                    //抓URL
                    $rules = [
                        'name' => ['#shujuList_table > tbody > tr > td.t2 > a', 'text'],
                        'url' => ['#shujuList_table > tbody > tr > td.t2 > a', 'href'],
                    ];
                    $urlArray = QueryList::get($url)->rules($rules)->query()->getData();//抓取贷罗盘官方所有正常运维的平台数据
                    $planUrl = '';
                    foreach ($urlArray as $k=>$v){
                        if($v['name']==$buserData[0]['pinname']){
                            $planUrl = 'http://www.dailuopan.com'.$v['url'];
                            break;
                        }
                    }
                    if($planUrl){
                        $field = $this->zhua_data($planUrl);//抓取数据
                        if($field){
                            $field['buser_id'] = $buserData[0]['id'];
                            $field['dai_url'] = $planUrl;
                            $linkInfo = Db::name('buserinfo');
                            $infoData = $linkInfo->where(array('buser_id'=>$id))->count();
                            if($infoData){
                                $linkInfo->where(array('buser_id'=>$id))->update($field);
                            }else{
                                $linkInfo->insertGetId($field);
                            }
                    }
                }
            }
        }
    }
    /**
     * 抓取工商信息程序
     */
    public function zhua_data($url=''){
        if($url){
            //抓URL
            $rules = [
                'url' => ['#goLink', 'text'],
            ];
            $urlArray = QueryList::get($url)->rules($rules)->query()->getData();
            $field['url'] = $urlArray[0]['url'];
            //组织架构
            $rules = [
                'name'=>['.member-info  .list li', 'text','-.member-info  -.list -li -span'],
                'val'=>['.member-info  .list li span', 'text'],
            ];
            $jiaData = QueryList::get($url)->rules($rules)->queryData();
            $field['organization'] = json_encode($jiaData);
            //权利地图
            // 采集表头
            $tableHeader = QueryList::get($url)->find('body > div.wrapper.detailContainer.clearfix > div:nth-child(11) > div.detailGongshang.clearfix > div.gudong-info.clearfix > dl > dt:eq(0)')->find('span')->texts();
// 采集表的每行内容
            $tableRows = QueryList::get($url)->find('body > div.wrapper.detailContainer.clearfix > div:nth-child(11) > div.detailGongshang.clearfix > div.gudong-info.clearfix > dl > dd:EGT(0)')->map(function($row){
                return $row->find('span')->texts()->all();
            });
            $quanArray = array();
            $quanArray['tableHeader'] = $tableHeader;
            $quanArray['tableRows'] = $tableRows;
            $field['right'] = json_encode($quanArray);
            //评级
            $rules = [
                // 采集文章标题
                'val' => ['body > div.wrapper.detailContainer.clearfix > div.detailRate.detailBox.clearfix.mt15 > div.rate-track.clearfix .rate-track-data .rate-info dt',
                    'text','-.iconfont -.bijiao'],
            ];
            $jiData = QueryList::get($url)->rules($rules)->query()->getData();
            $composite = array();
            foreach ($jiData as $ko=>$vo){
                $val = '';
                if($vo['val']){
                    $val = str_replace(array("\r\n", "\r", "\n","\t"), array(' ','','',''), $vo['val']);
                }
                $composite[] = array(
                    'val'=>$val
                );
            }
            $field['composite'] = json_encode($composite);
//工商信息
            $rules = [
                'name'=>['.basic-info .tab:nth-child(1) .tit', 'text'],
                'val'=>['.basic-info .tab:nth-child(1) .text', 'text'],
            ];
            $gongData = QueryList::get($url)->rules($rules)->queryData();
            $rules = [
                'name'=>['.basic-info .tab:nth-child(2) .tit', 'text'],
                'val'=>['.basic-info .tab:nth-child(2) td:nth-child(2)', 'text'],
            ];
            $qiData = QueryList::get($url)->rules($rules)->queryData();
            $circles = array_merge($gongData,$qiData);;
            $field['circles'] = json_encode($circles);
            //健康度指
            $rules = [
                'key'=>['body > div.wrapper.detailContainer.clearfix > div:nth-child(10) > div.detailHealth.detailHFlow > div.right > div.info.clearfix > ul span.text',
                    'text','-i'],
                'val'=>['body > div.wrapper.detailContainer.clearfix > div:nth-child(10) > div.detailHealth.detailHFlow > div.right > div.info.clearfix > ul span.text i',
                    'text','']
            ];
            $jianData = QueryList::get($url)->rules($rules)->query()->getData();
            $healthy = array();
            foreach ($jianData as $ko=>$vo){
                $name = '';
                if($vo['key']){
                    $name = rtrim($vo['key'],' |');
                }

                $healthy[] = array('key'=>$name,'val'=>$vo['val']);
            }
            $field['healthy'] = json_encode($healthy);
            return $field;
        }
    }
    /**
     * 定时抓取任务
     */
 /*   public function timeInfoData()
    {
        //查询B端用户
        $linkBuser = Db::name('Buser');
        $where = 'NOT EXISTS (SELECT buser_id FROM `fa_buserinfo` AS i WHERE b.id = i.buser_id)';
        $buserData = $linkBuser->field('*')->alias('b')->where($where)->select();
        foreach ($buserData as $k=>$v){
            if($v['pinname']){
                $url = 'http://www.dailuopan.com/p2p/'.$v['pinname'];
                //评级
                $rules = [
                    // 采集文章标题
                    'name' => ['body > div.wrapper.detailContainer.clearfix > div.detailRate.detailBox.clearfix.mt15 > div.rate-track.clearfix span.tit',
                        'text'],
                    'data' => ['body > div.wrapper.detailContainer.clearfix > div.detailRate.detailBox.clearfix.mt15 > div.rate-track.clearfix  span.shuju  '
                        ,'text'],
                ];
                $jiData = QueryList::get($url)->rules($rules)->query()->getData();
                $composite = array();
                foreach ($jiData as $ko=>$vo){
                    $name = '';
                    $val = '';
                    if($vo['name']){
                        $name = str_replace(array("\r\n", "\r", "\n","\t"), "", $vo['name']);
                    }
                    if($vo['data']){
                        $val = str_replace(array("\r\n", "\r", "\n","\t"), "", $vo['data']);

                    }
                    $composite[] = array(
                        'name'=>$name,
                        'val'=>$val
                    );
                }
                $field['composite'] = json_encode($composite);
                $field['buser_id'] = $v['id'];
//工商信息
                $rules = [
                    'gongData'=>['.basic-info .tab', 'text'],
                ];
                $gongData = QueryList::get($url)->rules($rules)->find('table:eq(0)')->map(function($row){
                    return $row->find('td')->texts()->all();
                });
                $circles = array();
                $value = array('name'=>'','val'=>'');
                foreach ($gongData[0] as $ko=>$vo){
                    if($ko%2==0){
                        //名字
                        $gongName =  str_replace(array("\r\n", "\r", "\n","\t"), "", $vo);
                        $value['name'] = $gongName;
                    }else{
                        $value['val'] = $vo;
                        $circles[] = $value;
                    }
                }
                $field['circles'] = json_encode($circles);
                //健康度指
                $rules = [
                    'val'=>['body > div.wrapper.detailContainer.clearfix > div:nth-child(10) > div.detailHealth.detailHFlow > div.right > div.info.clearfix > ul span.text', 'text'],
                ];
                $jianData = QueryList::get($url)->rules($rules)->query()->getData();
                $healthy = array();
                foreach ($jianData as $ko=>$vo){
                    if($vo['val']){
                        $jianName = str_replace(array("\r\n", "\r", "\n","\t"), "", $vo['val']);
                        $healthy[] = array('val'=>$jianName);
                    }
                }
                $field['healthy'] = json_encode($healthy);
                $linkInfo = Db::name('buserinfo');
                $linkInfo->insertGetId($field);
            }
        }
    }*/
}